home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / lib / lsb / init-functions
Encoding:
Text File  |  2008-08-18  |  9.4 KB  |  370 lines

  1. # /lib/lsb/init-functions for Debian -*- shell-script -*-
  2. #
  3. #Copyright (c) 2002-08 Chris Lawrence
  4. #All rights reserved.
  5. #
  6. #Redistribution and use in source and binary forms, with or without
  7. #modification, are permitted provided that the following conditions
  8. #are met:
  9. #1. Redistributions of source code must retain the above copyright
  10. #   notice, this list of conditions and the following disclaimer.
  11. #2. Redistributions in binary form must reproduce the above copyright
  12. #   notice, this list of conditions and the following disclaimer in the
  13. #   documentation and/or other materials provided with the distribution.
  14. #3. Neither the name of the author nor the names of other contributors
  15. #   may be used to endorse or promote products derived from this software
  16. #   without specific prior written permission.
  17. #
  18. #THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. #IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. #ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  22. #LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. #CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. #SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  25. #BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. #WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. #OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. #EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  
  30. start_daemon () {
  31.     local force nice pidfile exec i
  32.     force=0
  33.     nice=0
  34.     pidfile=/dev/null
  35.  
  36.     OPTIND=1
  37.     while getopts fn:p: opt ; do
  38.         case "$opt" in
  39.             f)  force=1;;
  40.             n)  nice="$OPTARG";;
  41.             p)  pidfile="$OPTARG";;
  42.         esac
  43.     done
  44.     
  45.     shift $(($OPTIND - 1))
  46.     if [ "$1" = '--' ]; then
  47.         shift
  48.     fi
  49.  
  50.     exec="$1"; shift
  51.  
  52.     if [ $force = 1 ]; then
  53.         /sbin/start-stop-daemon --start --nicelevel $nice --quiet --startas $exec --pidfile /dev/null --oknodo -- "$@"
  54.     elif [ $pidfile ]; then
  55.         /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo --pidfile "$pidfile" -- "$@"
  56.     else
  57.         /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo -- "$@"
  58.     fi
  59. }
  60.  
  61. pidofproc () {
  62.     local pidfile line i pids= status specified pid
  63.     pidfile=
  64.     specified=
  65.     
  66.     OPTIND=1
  67.     while getopts p: opt ; do
  68.         case "$opt" in
  69.             p)  pidfile="$OPTARG"; specified=1;;
  70.         esac
  71.     done
  72.     shift $(($OPTIND - 1))
  73.  
  74.     base=${1##*/}
  75.     if [ ! "$specified" ]; then
  76.         pidfile="/var/run/$base.pid"
  77.     fi
  78.  
  79.     if [ -n "${pidfile:-}" -a -e "$pidfile" ]; then
  80.         read pid < "$pidfile"
  81.         if [ -n "${pid:-}" ]; then
  82.             if $(kill -0 "${pid:-}" 2> /dev/null); then
  83.                 echo "$pid"
  84.                 return 0
  85.             elif ps "${pid:-}" >/dev/null 2>&1; then
  86.                 echo "$pid"
  87.                 return 0 # program is running, but not owned by this user
  88.             else
  89.                 return 1 # program is dead and /var/run pid file exists
  90.             fi
  91.         fi
  92.     fi
  93.     if [ -x /bin/pidof -a ! "$specified" ]; then
  94.         status="0"
  95.         /bin/pidof -o %PPID -x $1 || status="$?"
  96.         if [ "$status" = 1 ]; then
  97.             return 3 # program is not running
  98.         fi
  99.         return 0
  100.     fi
  101.     return 4 # program or service is unknown
  102. }
  103.  
  104. # start-stop-daemon uses the same algorithm as "pidofproc" above.
  105. killproc () {
  106.     local pidfile sig status base i name_param is_term_sig
  107.     pidfile=
  108.     name_param=
  109.     is_term_sig=no
  110.  
  111.     OPTIND=1
  112.     while getopts p: opt ; do
  113.         case "$opt" in
  114.             p)  pidfile="$OPTARG";;
  115.         esac
  116.     done
  117.     shift $(($OPTIND - 1))
  118.  
  119.     base=${1##*/}
  120.     if [ ! $pidfile ]; then
  121.         name_param="--name $base --pidfile /var/run/$base.pid"
  122.     else
  123.         name_param="--pidfile $pidfile"
  124.     fi
  125.  
  126.     sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/')
  127.     sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
  128.     if [ -z "$sig" -o "$sig" = 15 -o "$sig" = TERM ]; then
  129.         is_term_sig=yes
  130.     fi
  131.     status=0
  132.     if [ ! "$is_term_sig" = yes ]; then
  133.         if [ -n "$sig" ]; then
  134.             /sbin/start-stop-daemon --stop --signal "$sig" --quiet $name_param || status="$?"
  135.         else
  136.             /sbin/start-stop-daemon --stop --quiet $name_param || status="$?"
  137.         fi
  138.     else
  139.         /sbin/start-stop-daemon --stop --quiet --oknodo $name_param || status="$?"
  140.     fi
  141.     if [ "$status" = 1 ]; then
  142.         if [ -n "$sig" ]; then
  143.             return 0
  144.         fi
  145.         return 3 # program is not running
  146.     fi
  147.  
  148.     if [ "$status" = 0 -a "$is_term_sig" = yes -a "$pidfile" ]; then
  149.         pidofproc -p "$pidfile" "$1" >/dev/null || rm -f "$pidfile"
  150.     fi
  151.     return 0
  152. }
  153.  
  154. # Return LSB status
  155. status_of_proc () {
  156.     local pidfile daemon name status
  157.  
  158.     pidfile=
  159.     OPTIND=1
  160.     while getopts p: opt ; do
  161.         case "$opt" in
  162.             p)  pidfile="$OPTARG";;
  163.         esac
  164.     done
  165.     shift $(($OPTIND - 1))
  166.  
  167.     if [ -n "$pidfile" ]; then
  168.         pidfile="-p $pidfile"
  169.     fi
  170.     daemon="$1"
  171.     name="$2"
  172.  
  173.     status="0"
  174.     pidofproc $pidfile $daemon >/dev/null || status="$?"
  175.     if [ "$status" = 0 ]; then
  176.         log_success_msg "$name is running"
  177.         return 0
  178.     else
  179.         log_failure_msg "$name is not running"
  180.         return $status
  181.     fi
  182. }
  183.  
  184. log_use_fancy_output () {
  185.     TPUT=/usr/bin/tput
  186.     EXPR=/usr/bin/expr
  187.     if [ -t 1 ] && [ "x$TERM" != "" ] && [ "x$TERM" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1 && $TPUT setaf 1 >/dev/null 2>&1; then
  188.         [ -z $FANCYTTY ] && FANCYTTY=1 || true
  189.     else
  190.         FANCYTTY=0
  191.     fi
  192.     case "$FANCYTTY" in
  193.         1|Y|yes|true)   true;;
  194.         *)              false;;
  195.     esac
  196. }
  197.  
  198. log_success_msg () {
  199.     if [ -n "${1:-}" ]; then
  200.         log_begin_msg $@
  201.     fi
  202.     log_end_msg 0
  203. }
  204.  
  205. log_failure_msg () {
  206.     if [ -n "${1:-}" ]; then
  207.         log_begin_msg $@
  208.     fi
  209.     log_end_msg 1 || true
  210. }
  211.  
  212. log_warning_msg () {
  213.     if [ -n "${1:-}" ]; then
  214.         log_begin_msg $@
  215.     fi
  216.     log_end_msg 255 || true
  217. }
  218.  
  219. #
  220. # NON-LSB HELPER FUNCTIONS
  221. #
  222. # int get_lsb_header_val (char *scriptpathname, char *key)
  223. get_lsb_header_val () {
  224.         if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  225.                 return 1
  226.         fi
  227.         LSB_S="### BEGIN INIT INFO"
  228.         LSB_E="### END INIT INFO"
  229.         sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  230. }
  231.  
  232. # int log_begin_message (char *message)
  233. log_begin_msg () {
  234.     if [ -z "${1:-}" ]; then
  235.         return 1
  236.     fi
  237.     echo -n "$@"
  238. }
  239.  
  240. # Sample usage:
  241. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  242. #
  243. # On Debian, would output "Starting GNOME Login Manager: gdm"
  244. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  245. #
  246. # If the second argument is omitted, logging suitable for use with
  247. # log_progress_msg() is used:
  248. #
  249. # log_daemon_msg "Starting remote filesystem services"
  250. #
  251. # On Debian, would output "Starting remote filesystem services:"
  252. # On Ubuntu, would output " * Starting remote filesystem services..."
  253.  
  254. log_daemon_msg () {
  255.     if [ -z "${1:-}" ]; then
  256.         return 1
  257.     fi
  258.     log_daemon_msg_pre "$@"
  259.  
  260.     if [ -z "${2:-}" ]; then
  261.         echo -n "$1:"
  262.         return
  263.     fi
  264.     
  265.     echo -n "$1: $2"
  266.     log_daemon_msg_post "$@"
  267. }
  268.  
  269. # #319739
  270. #
  271. # Per policy docs:
  272. #
  273. #     log_daemon_msg "Starting remote file system services"
  274. #     log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  275. #     log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  276. #     log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  277. #     log_end_msg 0
  278. #
  279. # You could also do something fancy with log_end_msg here based on the
  280. # return values of start-stop-daemon; this is left as an exercise for
  281. # the reader...
  282. #
  283. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  284. log_progress_msg () {
  285.     if [ -z "${1:-}" ]; then
  286.         return 1
  287.     fi
  288.     echo -n " $@"
  289. }
  290.  
  291.  
  292. # int log_end_message (int exitstatus)
  293. log_end_msg () {
  294.     # If no arguments were passed, return
  295.     if [ -z "${1:-}" ]; then
  296.         return 1
  297.     fi
  298.  
  299.     retval=$1
  300.  
  301.     log_end_msg_pre "$@"
  302.  
  303.     # Only do the fancy stuff if we have an appropriate terminal
  304.     # and if /usr is already mounted
  305.     if log_use_fancy_output; then
  306.         RED=`$TPUT setaf 1`
  307.         YELLOW=`$TPUT setaf 3`
  308.         NORMAL=`$TPUT op`
  309.     else
  310.         RED=''
  311.         YELLOW=''
  312.         NORMAL=''
  313.     fi
  314.  
  315.     if [ $1 -eq 0 ]; then
  316.         echo "."
  317.     elif [ $1 -eq 255 ]; then
  318.         /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
  319.     else
  320.         /bin/echo -e " ${RED}failed!${NORMAL}"
  321.     fi
  322.     log_end_msg_post "$@"
  323.     return $retval
  324. }
  325.  
  326. log_action_msg () {
  327.     echo "$@."
  328. }
  329.  
  330. log_action_begin_msg () {
  331.     echo -n "$@..."
  332. }
  333.  
  334. log_action_cont_msg () {
  335.     echo -n "$@..."
  336. }
  337.  
  338. log_action_end_msg () {
  339.     log_action_end_msg_pre "$@"
  340.     if [ -z "${2:-}" ]; then
  341.         end="."
  342.     else
  343.         end=" ($2)."
  344.     fi
  345.  
  346.     if [ $1 -eq 0 ]; then
  347.         echo "done${end}"
  348.     else
  349.         if log_use_fancy_output; then
  350.             RED=`$TPUT setaf 1`
  351.             NORMAL=`$TPUT op`
  352.             /bin/echo -e "${RED}failed${end}${NORMAL}"
  353.         else
  354.             echo "failed${end}"
  355.         fi
  356.     fi
  357.     log_action_end_msg_post "$@"
  358. }
  359.  
  360. # Hooks for /etc/lsb-base-logging.sh
  361. log_daemon_msg_pre () { :; }
  362. log_daemon_msg_post () { :; }
  363. log_end_msg_pre () { :; }
  364. log_end_msg_post () { :; }
  365. log_action_end_msg_pre () { :; }
  366. log_action_end_msg_post () { :; }
  367.  
  368. FANCYTTY=
  369. [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true
  370.